package com.matt.foundation.model;

import com.matt.commons.exception.BusinessException;
import lombok.Getter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DailyRange {

    @Getter
    private final Date dayBegin;

    @Getter
    private final Date dayEnd;

    private DailyRange(Date dayBegin, Date dayEnd){
        this.dayBegin = dayBegin;
        this.dayEnd = dayEnd;
    }

    public static DailyRange of(Date daily){
        //计算daily时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(daily);
        String subDate = format.substring(0, 10);

        //定义每天的24h时间范围
        String beginTime = subDate + " 00:00:00";
        String endTime = subDate + " 23:59:59";

        Date dayBegin = null;
        Date dayEnd = null;

        try {
            dayBegin = dateFormat.parse(beginTime);
            dayEnd = dateFormat.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.DAY_TOO_BIG, "日期参数处理错误");
        }

        return new DailyRange(dayBegin, dayEnd);
    }

}
